package com.peng.leetcode.tree;

/**
 * IsBalanced3
 * <p>
 * Created on 2023/1/23
 *
 * @author lupeng
 */
public class IsBalanced3 {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        } else {
            return Math.abs(height(root.left) - height(root.right)) <=1 && isBalanced(root.left) && isBalanced(root.right);
        }
    }

    int height(TreeNode n) {
        if (n == null) {
            return 0;
        } else {
            return Math.max(height(n.left), height(n.right)) + 1;
        }
    }
}
